#include"serialize/mission.h"

//mission definition
Mission::Mission()
{  
    id = 0;
    name = "";
    description = "";
    energy = 0;
    difficulty = 1;
    completeness = 0;
    reward_exp = 0;
    reward_cash = 0;
    random_reward_item_count = 0;
    random_reward_item = 0;
    buttonStr = "";
    animation = 0;
}

Mission::Mission(qint32 _id,QString _name,QString _description,qint32 _energy
                 ,quint8 _difficulty,quint8 _completeness,qint32 _reward_exp,qint32 _reward_cash
                 ,qint32 _random_reward_item_count,RewardItem* _random_reward_item
                 ,QString _buutonStr,quint8 _animation)
{
    id = _id;
    name = _name;
    description = _description;
    energy = _energy;
    difficulty = _difficulty;
    completeness = _completeness;
    reward_exp = _reward_exp;
    reward_cash = _reward_cash;
    random_reward_item_count = _random_reward_item_count;
    if(random_reward_item!=0)
        delete[] random_reward_item;
    random_reward_item = new RewardItem[random_reward_item_count];
    for(qint32 i=0;i<random_reward_item_count;i++)
        random_reward_item[i] = _random_reward_item[i];
    buttonStr = _buutonStr;
    animation = _animation;
}

Mission::Mission(const Mission& m)
{
    id = m.id;
    name = m.name;
    description = m.description;
    energy = m.energy;
    difficulty = m.difficulty;
    completeness = m.completeness;
    reward_exp = m.reward_exp;
    reward_cash = m.reward_cash;
    random_reward_item_count = m.random_reward_item_count;
    if(random_reward_item!=0)
        delete[] random_reward_item;
    random_reward_item = new RewardItem[random_reward_item_count];
    for(qint32 i=0;i<random_reward_item_count;i++)
        random_reward_item[i] = m.random_reward_item[i];
    buttonStr = m.buttonStr;
    animation = m.animation;
}

Mission& Mission::operator=(const Mission& m)
{
    if(&m == this)
        return *this;

    delete[] random_reward_item;

    id = m.id;
    name = m.name;
    description = m.description;
    energy = m.energy;
    difficulty = m.difficulty;
    completeness = m.completeness;
    reward_exp = m.reward_exp;
    reward_cash = m.reward_cash;
    random_reward_item_count = m.random_reward_item_count;
    if(random_reward_item!=0)
        delete[] random_reward_item;
    random_reward_item = new RewardItem[random_reward_item_count];
    for(qint32 i=0;i<random_reward_item_count;i++)
        random_reward_item[i] = m.random_reward_item[i];
    buttonStr = m.buttonStr;
    animation = m.animation;
    return *this;
}

Mission::~Mission()
{
    if(random_reward_item!=0)
        delete[] random_reward_item;
}

ostream& operator <<(ostream& os,Mission& m)
{
    os<<"******start******"<<endl;
    os<<"Mission info:"<<endl;
    os<<"id:"<<(int)m.id<<endl;
    os<<"name:"<<m.name.toStdString()<<endl;
    os<<"descriptionion:"<<m.description.toStdString()<<endl;
    os<<"energy:"<<(int)m.energy<<endl;
    os<<"difficulty:"<<(int)m.difficulty<<endl;
    os<<"completeness:"<<(int)m.completeness<<endl;
    os<<"reward_exp:"<<(int)m.reward_exp<<endl;
    os<<"reward_cash:"<<(int)m.reward_cash<<endl;
    os<<"random_reward_item_count:"<<(int)m.random_reward_item_count<<",They are: ";
    for(qint32 i=0;i<m.random_reward_item_count;i++)
    {
        os<<"("<<(int)m.random_reward_item[i].id<<","<<(int)m.random_reward_item[i].possibility<<
         ","<<(int)m.random_reward_item[i].type<<")";
        if(i!=m.random_reward_item_count-1)
            os<<",";
    }
    os<<endl;
    os<<"buttonString:"<<m.buttonStr.toStdString()<<endl;
    os<<"animationID:"<<(int)m.animation<<endl;
    os<<"******end******"<<endl;

    return os;
}

QDataStream& operator<<(QDataStream& ds,Mission& m)
{
    ds<<m.id;
    ds<<m.name.toUtf8().data();
    ds<<m.description.toUtf8().data();
    ds<<m.energy;
    ds<<m.difficulty;
    ds<<m.completeness;
    ds<<m.reward_exp;
    ds<<m.reward_cash;
    ds<<m.random_reward_item_count;
    for(qint32 i=0;i<m.random_reward_item_count;i++)
    {
        ds<<m.random_reward_item[i].id;
        ds<<m.random_reward_item[i].possibility;
        ds<<m.random_reward_item[i].type;
    }
    ds<<m.buttonStr.toUtf8().data();
    ds<<m.animation;

    return ds;
}

QDataStream& operator>>(QDataStream& ds,Mission& m)
{
    ds>>m.id;
    char* temp;
    ds>>temp;
    m.name = QString::fromUtf8(temp);
    delete[] temp;
    ds>>temp;
    m.description = QString::fromUtf8(temp);
    delete[] temp;
    ds>>m.energy;
    ds>>m.difficulty;
    ds>>m.completeness;
    ds>>m.reward_exp;
    ds>>m.reward_cash;
    ds>>m.random_reward_item_count;
    if(m.random_reward_item!=0)
        delete[] m.random_reward_item;
    m.random_reward_item = new RewardItem[m.random_reward_item_count];
    for(qint32 i=0;i<m.random_reward_item_count;i++)
    {
        ds>>m.random_reward_item[i].id;
        ds>>m.random_reward_item[i].possibility;
        ds>>m.random_reward_item[i].type;
    }
    ds>>temp;
    m.buttonStr = QString::fromUtf8(temp);
    delete[] temp;
    ds>>m.animation;

    return ds;
}


